package com.helpdo.task.controller;

import java.util.List;
import java.util.Map;

import com.helpdo.task.domain.TaskDO;
import com.helpdo.task.service.TaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.helpdo.common.utils.PageUtils;
import com.helpdo.common.utils.Query;
import com.helpdo.common.utils.R;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-02-27 14:52:44
 */
 
@Controller
@RequestMapping("/task")
public class TaskController {
	@Autowired
	private TaskService taskService;
	
	@GetMapping()
	@RequiresPermissions("task:task")
	String Task(){
	    return "task/task";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("task:task")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<TaskDO> taskList = taskService.list(query);
		int total = taskService.count(query);
		PageUtils pageUtils = new PageUtils(taskList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("task:add")
	String add(){
	    return "task/add";
	}

	@GetMapping("/edit/{taskId}")
	@RequiresPermissions("task:edit")
	String edit(@PathVariable("taskId") Long taskId,Model model){
		TaskDO task = taskService.get(taskId);
		model.addAttribute("task", task);
	    return "task/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("task:add")
	public R save( TaskDO task){
		if(taskService.save(task)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("task:edit")
	public R update( TaskDO task){
		taskService.update(task);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("task:remove")
	public R remove( Long taskId){
		if(taskService.remove(taskId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("task:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] taskIds){
		taskService.batchRemove(taskIds);
		return R.ok();
	}
	
}
